package com.tencent.wxcloudrun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tencent.wxcloudrun.controller.utils.ResultResponse;
import com.tencent.wxcloudrun.domain.Order;
import com.tencent.wxcloudrun.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping
    public ResultResponse addOrder(@RequestBody Order order) {
        boolean result = orderService.save(order);
        return ResultResponse.ok(result, "预订成功");
    }

    @GetMapping
    public ResultResponse listOrders(@RequestParam(name = "ot") String ot) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getOrderStatus,ot);
        return ResultResponse.ok(orderService.list(wrapper), false);
    }

    @PutMapping
    public ResultResponse setOrderStatus(@RequestBody Order order) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",order.getId());
        updateWrapper.set("order_status",order.getOrderStatus());
        return ResultResponse.ok(orderService.update(updateWrapper),"取消成功");
    }
}
